<?php
namespace app\crm\controller;
use think\Loader;
use think\Db;

class Users extends CrmController
{   
    /**
     * 用户列表界面
     * @return [type] [description]
     */
    public function usersList()
    {	
		//分配增删改的权限
		$menuname = "Users";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];		
		$this->assign("username",$_SESSION['user_info']['username']);
        $this->assign("priv",$priv);
		
		$roleList = Loader::model('Role')->getRoleList();
		$this->assign('roleList',$roleList);
		
		$result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result);
        $this->assign('options', $result);
		// 模板输出
        return $this->fetch('usersList');
    }

    /**
     * 获取用户列表资料
     * @return [type] [description]
     */
    public function userDataList()
    {
        $page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
        //需先判断传值是否为空
        $where = "(1 ";
        $where .= empty($_REQUEST["username"])?"":" AND username = '".$_REQUEST["username"]."'";
        $where .= empty($_REQUEST["cn_name"])?"":" AND cn_name like '%".$_REQUEST["cn_name"]."%'";
        $where .= empty($_REQUEST["phone"])?"":" AND phone = '".$_REQUEST["phone"]."'";
        $where .= empty($_REQUEST["dept_id"])?"":" AND d_id = '".$_REQUEST["dept_id"]."'";
		$where .= empty($_REQUEST["r_id"])?"":" AND r_id = '".$_REQUEST["r_id"]."'";
        $where .= " )";
        // print_r($where);exit;
        $arrData = Loader::model('Users')->getUserData($where,$page,$limit);
        echo json_encode($arrData);
    }
	
    /**
     * 添加用户的界面
     */
	public function addUsers()
    {
        $roleList = Loader::model('Role')->getRoleList();
		$this->assign('roleList',$roleList);
        $result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result);
        $this->assign('options', $result);
        // 模板输出
        return $this->fetch('addUsers');
    }
	
	/**
     * 批量添加用户的界面
     */
	public function batchAddUsers()
    {
        $roleList = Loader::model('Role')->getRoleList();
		$this->assign('roleList',$roleList);
        $result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result);
        $this->assign('options', $result);
        // 模板输出
        return $this->fetch('batchAddUsers');
    }

	
    /**
     * 添加用户
     * @return [type] [description]
     */
    public function insertuser()
    {
        Loader::model('Users')->insertUser($_REQUEST);
    }
	
	/**
     * 批量添加用户
     * @return [type] [description]
     */
    public function batchInsertUser()
    {
        Loader::model('Users')->batchInsertUser();
    }
	
	/**
     * 批量修改用户部门
     * @return [type] [description]
     */
    public function batchChangeDepartment()
    {
        Loader::model('Users')->batchChangeDepartment();
    }
	
	/**
     * 批量清空密码
     * @return [type] [description]
     */
    public function clearPassword()
    {
        Loader::model('Users')->clearPassword();
    }
	
    /**
     * 编辑用户的界面
     * @return [type] [description]
     */
	public function editUsers()
    {
        $username = $_REQUEST["username"];
        $userInfo = Loader::model('Users')->getUserInfo($username);
        $this->assign('userInfo',$userInfo);    
        $roleList = Loader::model('Role')->getRoleList();
        $this->assign('roleList',$roleList);
        $result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result,$userInfo["d_id"]);
        //dump($result);die;

        $this->assign('options', $result);
		// 模板输出
        return $this->fetch('editUsers');
    }

    /**
     * 更新用户
     * @return [type] [description]
     */
    public function updateUser()
    {
        Loader::model('Users')->updateUser($_REQUEST["username"],$_POST);

    }

    /**
     * 删除用户
     * @return [type] [description]
     */
    public function deleteUsrs()
    {
        $username = $_REQUEST["username"];
        Loader::model('Users')->deleteUsrs($username);
        
    }

	/**
     * 批量设置IP
     * @return [type] [description]
     */
    public function batchUpdateIP()
    {
        Loader::model('Users')->batchUpdateIP();

    }



}